using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Core;
using Core.LockStep;

public class BattleScene : BaseScene
{
    private string SaveDir
    {
        get
        {
            return $"{Application.dataPath}/../Record";
        }
    }

    public string saveFileName = "battleRecord";

    protected override void OnInit(object data)
    {
        var mainCamera = new GetMainCamera().Publish().mainCamera;
        var mainCameraPos = GetReference<Transform>("mainCameraPos");
        mainCamera.transform.position = mainCameraPos.transform.position;
        mainCamera.transform.rotation = mainCameraPos.transform.rotation;

        new OpenPanel(PanelPath.BattlePanel).Publish();

        Management.World.AddFeature<BattleFeature>();

        Management.World.AddFeature<Core.TcpFeature>();
        Management.World.AddFeature<UdpClientFeature>();

        Subscribe<StartBattle>(StartBattle);
        Subscribe<LoadBattleRecord>(LoadBattleRecord);
        Subscribe<SaveBattleRecord>(SaveBattleRecord);

        
    }

    private void StartBattle(ref StartBattle data)
    {
        var battleInfo = new BattleInfoData();
        battleInfo.playerId = 1;
        battleInfo.playerCount = 1;
        battleInfo.playerIds = new List<int>() { battleInfo.playerId };

        new StartLockStep(battleInfo, 0, true, true).Publish();
    }

    private void LoadBattleRecord(ref LoadBattleRecord data)
    {
        new LoadLockStepData($"{SaveDir}/{saveFileName}.bytes").Publish();
    }

    private void SaveBattleRecord(ref SaveBattleRecord data)
    {
        new StopLockStep().Publish();

        new SaveLockStepData($"{SaveDir}/{saveFileName}.bytes", SaveLockStepData.SaveType.Bytes).Publish();
        new SaveLockStepData($"{SaveDir}/{saveFileName}.txt", SaveLockStepData.SaveType.String).Publish();
    }

    private void OnDestroy()
    {
        Management.World.RemoveFeature<BattleFeature>();
    }
}